'use strict';

let debug = require('debug')('district');

let daoMongo = new require('./mongo/District.js')();
let daoRedis = require('./redis/district.js');
let daoEs = require('./es/district.js');

let DaoDispatcher = function () {
};
module.exports = new DaoDispatcher();
let theProto = DaoDispatcher.prototype;

/**
 * 添加单个entity add single district
 * save to mongodb, then update redis and es
 * @param json
 * @returns {*}
 */
theProto.addOne = function*(params) {
    debug('DaoDispatcher', params);

    // step1: save to mongodb
    let result = yield daoMongo.saveOne(params);
    if (result.errCode !== 0) {
        return result;
    }

    // step2: cache it to redis
    yield daoRedis.save(result.result);
    //yield daoRedis.testLua();
    //yield daoRedis.fun();
    //yield daoRedis.add();
    //yield daoRedis.distance();

    // step3: sync to elastic search
    yield daoEs.create(result.result);

    return result;
};

theProto.addMany = function*(params) {
    let result = yield  daoMongo.saveMany(params);
    if (result.errCode === 0) {
        let list = result.result.list;
        for (let i = 0; i < list.length; i++) {
            yield daoRedis.save(list[i]);
            yield daoEs.create(list[i]);
        }
    }

    return result;
};

/**
 *
 * @param params
 * @returns {*}
 */
theProto.findById = function*(params) {
    let result;
    let id = params.id;
    result = yield daoRedis.get(id);

    if (result === null) {
        result = daoMongo.findById(id);
        daoRedis.save(result);
    }

    return result;
};

/**
 * @param params
 * 首先在redis找父节点
 * |___找不到--mongodb中找,并存入redis
 * |___找到---在redis中找子节点(subid)
 *    |___找到
 *    |___找不到
 *        |___mongodb中找,并存入redis
 */
theProto.findByPid = function*(params) {
    // 从mongo中查询child,并存入redis
    function* findInMongoAndSave(pid) {
        let children = daoMongo.findByPid(pid);
        for (let i = 0; i < children.length; i++) {
            yield daoRedis.save(result[i]);
        }
        return children;
    }

    let result;
    let pid = params.pid;
    if (pid === null) {
        result = yield findInMongoAndSave(pid);
    } else {
        result = yield daoRedis.getByPid(pid);
        if (!result) {
            result = yield findInMongoAndSave(pid);
        }
    }

    return result;
};

/**
 *
 * @param params {lng:xx,lat:xx,radius:xx}
 */
theProto.findByDistance = function* (params) {
    let result = yield daoEs.search({body: params});
    return result;
};

/**
 *
 * @param params {keyword:xxx}
 */
theProto.quickSearch = function* (params) {
    let q = params.keyword;
    let result = yield daoEs.search({q: q});
    return result;
};

theProto.findByKeyword = function* (params) {
    let q = params.keyword;
    let body = {
        query: {
            term: {_all: q}
        }
    };
    let result = yield daoEs.search({body: body});
    return result;
};


/**
 * update 操作
 * @param params {name:xxx,id:xxx}
 */
theProto.updateName = function*(params) {
    var query = {_id: params.id};
    let update = {$set: {name: params.name}};
    let option = {new: true};
    let result = yield daoMongo.findAndModify(query, update, option);
    if (result.errCode !== 0) {
        return result;
    }

    result = result.result;

    // update redis
    yield daoRedis.save(result);

    // update es
    yield daoEs.update(result);
    return result;
};